package com.dm0858.bianmin.mine.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.RecyclerView;

public class BaseViewHolder extends RecyclerView.ViewHolder {


    private Context mContext;
    private SparseArray<View> mViews;

    public BaseViewHolder(Context mContext, @NonNull View itemView) {
        super(itemView);
        this.mContext = mContext;
        mViews = new SparseArray<>();
    }

    /**
     * 通过viewId获取控件
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if (view == null){
            view = itemView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T) view;
    }

    public ImageView getImageView(int viewId) {
        ImageView imageView = getView(viewId);
        return imageView;
    }


    /********************TextView相关********************/

    /**
     * 设置文本文字
     * @param viewId
     * @param value
     * @return
     */
    public BaseViewHolder setText(int viewId, CharSequence value){
        TextView textView = getView(viewId);
        textView.setText(value + "");
        return this;
    }

    /**
     * 设置文本文字
     * @param viewId
     * @param value
     * @return
     */
    public BaseViewHolder setText(int viewId, String value){
        TextView textView = getView(viewId);
        textView.setText(value + "");
        return this;
    }

    /***
     * 设置文本文字
     * @param viewId
     * @param value
     * @return
     */
    public BaseViewHolder setText(int viewId, int value){
        TextView textView = getView(viewId);
        textView.setText(value + "");
        return this;
    }

    /**
     * 设置文本大小
     * @param viewId
     * @param textSize
     * @return
     */
    public BaseViewHolder setTextSize(int viewId, int textSize){
        TextView textView = getView(viewId);
        textView.setTextSize(textSize);
        return this;
    }

    /**
     * 设置文本颜色
     * @param viewId
     * @param textColor
     * @return
     */
    public BaseViewHolder setTextColor(int viewId, @ColorInt int textColor){
        TextView textView = getView(viewId);
        textView.setTextColor(textColor);
        return this;
    }

    /**
     * 设置文本颜色
     * @param viewId
     * @param colorRes
     * @return
     */
    public BaseViewHolder setTextColorRes(int viewId, @ColorRes int colorRes){
        TextView textView = getView(viewId);
        textView.setTextColor(mContext.getResources().getColor(colorRes));
        return this;
    }


    /**
     * 设置文本的背景颜色
     * @param viewId
     * @param backgroundColor
     * @return
     */
    public BaseViewHolder setTextBackgroundColor(int viewId, @ColorInt int backgroundColor){
        TextView textView = getView(viewId);
        textView.setBackgroundColor(backgroundColor);
        return this;
    }

    /**
     * 设置文本的背景颜色
     * @param viewId
     * @param colorRes
     * @return
     */
    public BaseViewHolder setTextBackgroundColorRes(int viewId, @ColorRes int colorRes){
        TextView textView = getView(viewId);
        textView.setBackgroundColor(mContext.getResources().getColor(colorRes));
        return this;
    }

    /**
     * 设置文本的背景样式
     * @param viewId
     * @param drawable
     * @return
     */
    public BaseViewHolder setTextBackground(int viewId,Drawable drawable) {
        TextView textView = getView(viewId);
        textView.setBackground(drawable);
        return this;
    }

    public BaseViewHolder setTextVisibility(int viewId, int visible){
        TextView textView = getView(viewId);
        textView.setVisibility(visible);
        return this;
    }
    /**
     * 设置Drawable
     * @param viewId
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @return
     */
    public BaseViewHolder setTextDrawable(int viewId, Drawable left, Drawable top, Drawable right, Drawable bottom){
        TextView textView = getView(viewId);
        textView.setCompoundDrawables(left,top,right,bottom);
        return this;
    }

    public BaseViewHolder setCardCornerRadius(int viewId, @DrawableRes int drawableRes){
        CoordinatorLayout coordinatorLayout = getView(viewId);
        coordinatorLayout.setBackground(mContext.getResources().getDrawable(drawableRes));
        return this;
    }

    /********************ImageView相关********************/

    public BaseViewHolder setImageResource(int viewId,@DrawableRes int drawable){
        ImageView imageView = getView(viewId);
        imageView.setImageResource(drawable);
        return this;
    }

    public BaseViewHolder setImageResource(int viewId,Drawable drawable){
        ImageView imageView = getView(viewId);
        imageView.setImageDrawable(drawable);
        return this;
    }

    /********************是否可见********************/

    public BaseViewHolder setVisibility(int viewId,int i){
        View view = getView(viewId);
        view.setVisibility(i);
        return this;
    }

    /********************LinearLayout相关********************/

    public BaseViewHolder setLinearLayoutVisibility(int viewId, int visible){
        LinearLayout linearLayout = getView(viewId);
        linearLayout.setVisibility(visible);
        return this;
    }

    /********************控件背景相关********************/
    public BaseViewHolder setBackground(int viewId,Drawable drawable) {
        View view = getView(viewId);
        view.setBackground(drawable);
        return this;
    }

    /********************常用监听********************/

    public BaseViewHolder setOnClickListener(int viewId, View.OnClickListener listener){
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    public BaseViewHolder setOnLongClickListener(int viewId,View.OnLongClickListener longClickListener) {
        View view = getView(viewId);
        view.setOnLongClickListener(longClickListener);
        return this;
    }
}
